package org.reenscape.remote;
import org.reenscape.updater.*;
import org.reenscape.*;
import org.reenscape.model.*;
import java.io.*;
import java.net.*;
/**
*@author Troy Osborne
*/
public class Console implements Runnable{

	public Console(InputStream i, OutputStream o){
		origout = o;
		in = new BufferedReader(new InputStreamReader(i));
		out = new PrintWriter(o);
	}
	
	public void run(){
		try{
			while(true){
				processCommand(in.readLine());
			}
		}catch(Exception ex){}
	}
	
	public void processCommand(String cmd){
	
		int cs = -1;
		for(int i=0; i<commands.length; i++){
			if(cmd.startsWith(commands[i])){
				cs = i;
				break;
			}
		}
		
		switch(cs){
			case -1:
				out.println("Invalid command. \"" + cmd + "\"");
				out.flush();
			break;
				
			default:
				out.println("Unhandled command. \"" + cmd + "\"");
				out.flush();
			break;
			
			case 0:
				for(Player p : ClientUpdater.players){
					if(p != null){
						p.disconnected = true;
					}
				}
				out.println("All users disconnected.");
				out.flush();
			break;
			
			case 1:
				out.println("Server shutting down...");
				out.flush();
				for(Player p : ClientUpdater.players){
					if(p != null){
						p.disconnected = true;
					}
				}
				System.exit(0);
			break;
			
			case 2:
				//Server.clientUpdater.getPlayerCount();
				out.println("Currently "+ClientUpdater.getPlayerCount()+" people playing on this world.");
				out.flush();
			break;
			
			case 3:
				try{
					ClientUpdater.kickNick = cmd.substring(5);
					out.println(cmd.substring(5)+" has been kicked.");
					out.flush();
				} catch(Exception e){
					out.println("Invalid syntax, use as 'kick username'");
					out.flush();
				}
			break;
			
			case 4:
			case 5:
				out.println("Command not yet implemented.");
				out.flush();
			break;
			
			case 6:
				try{
					ClientUpdater.messageToAll = cmd.substring(5);
				} catch(Exception e){
					out.println("Invalid syntax, use as 'yell msg'");
					out.flush();
				}
			break;
			
			case 7:
				try{
					ClientUpdater.updateSeconds = (Integer.parseInt(cmd.substring(7)) + 1);
					ClientUpdater.updateAnnounced = false;
					ClientUpdater.updateRunning = true;
					ClientUpdater.updateStartTime = System.currentTimeMillis();
					out.println("Update started. Time: "+(Integer.parseInt(cmd.substring(7))));
					out.flush();
				} catch(Exception e){ out.println("You need to specify a number of seconds."); out.flush();}
			break;
			
			case 8:
				printHelp();
			break;
		}	
	}
	
	public void printHelp(){
		out.println();
		out.println("-- commands");
		out.println("bootall - disconnect all users");
		out.println("kill - kill server");
		out.println("players - show amount of players on");
		out.println("kick - kick specified user");
		out.println("ban - ban specified user");
		out.println("ipban - ban specified hostname/IP");
		out.println("yell - send specified message to everyone");
		out.println("update - initialize update with specified seconds");
		out.println();
		out.flush();
	}

	private String[] commands = {"bootall","kill","players","kick","ban", "ipban","yell","update", "help"};

	private BufferedReader in;
	private PrintWriter out;
	private OutputStream origout;

}